import numpy as np
from PIL import Image

def predict_image(image_path):
    img = Image.open(image_path).resize(IMG_SIZE)
    img_array = np.array(img)
    img_array = preprocess_input(img_array)  # 与训练相同的预处理
    img_array = np.expand_dims(img_array, axis=0)  # 添加批次维度

    predictions = model.predict(img_array)
    predicted_class = np.argmax(predictions[0])
    return predicted_class

# 示例
print("Predicted class:", predict_image('new_image.jpg'))


loaded_model = tf.keras.models.load_model('my_multiclass_model.h5')

# 示例：预测单张图片
img_path = 'test_image.jpg'
img = tf.keras.preprocessing.image.load_img(img_path, target_size=IMG_SIZE)
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)  # 扩展维度为 (1, 224, 224, 3)

predictions = loaded_model.predict(img_array)
predicted_class = class_names[tf.argmax(predictions[0])]
print("预测类别:", predicted_class)